﻿using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using SqlSugar;
using Hiver.Util.SqlServer.SqlSugar;

namespace Hiver.Data
{
    /// <summary>
    /// 数据仓库操作类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Repository<T> where T : class, new()
    {
        /// <summary>
        /// 实例化数据库
        /// </summary>
        public SimpleClient<T> db = new SimpleClient<T>(SqlSugarConn.GetInstance());

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<T> GetSearchPageList(Expression<Func<T, bool>> query, int pageIndex, int pageSize)
        {
            PageModel page = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
            List<T> data = db.GetPageList(query, page);
            return data;
        }

        /// <summary>
        /// 获得数据分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<T> GetPageList(int pageIndex, int pageSize)
        {
            PageModel page = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
            Expression<Func<T, bool>> ex = (it => 1 == 1);
            List<T> data = db.GetPageList(ex, page);
            return data;
        }

        /// <summary>
        /// 获得所有数据
        /// </summary>
        /// <returns></returns>
        public List<T> GetList()
        {
            return db.GetList();
        }

        /// <summary>
        /// 通过id获得记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Get(dynamic id)
        {
            return db.GetById(id);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(T entity)
        {
            return db.InsertReturnIdentity(entity);
        }

        /// <summary>
        /// 添加字符串为主键的实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool AddByString(T entity)
        {
            return db.Insert(entity);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            return db.Update(entity);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(dynamic id)
        {
            return db.DeleteById(id);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Dels(dynamic[] ids)
        {
            return db.DeleteByIds(ids);
        }
    }
}